<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>组件间通信</title>
</head>

<body>
    <div id="app">
        <one-component></one-component>
        <two-component></two-component>
    </div>
</body>
<script src="../day06/lib/vue.js"></script>
<script>
    let eventBus = new Vue()
    let oneComponent = {
        data() {
            return {
                name: '组件A',
                blood: 150
            }
        },
        template:
            `
        <div style="border:1px solid red;">
            <div>我是{{name}},我当前的血量是{{blood}}</div>
            <button @click="attackB">点我给对方造成1点伤害</button>

        </div>
        `,
        created() {
            eventBus.$on('getAttackFromB', val => {
                this.blood -= val
            })
        },
        methods: {
            attackB() {
                eventBus.$emit('getAttackFromA', 1)
            }
        }

    }
    let twoComponent = {
        data() {
            return {
                name: '组件B',
                blood: 100
            }
        },
        template:
            `
        <div style="border:1px solid green;">
            <div>我是{{name}},我当前的血量是{{blood}}</div>
            <button @click="attackA">点我给对方造成1.5点伤害</button>
        </div>
        `,
        methods: {
            attackA() {
                eventBus.$emit('getAttackFromB', 1.5)
            }
        },
        created() {
            eventBus.$on('getAttackFromA', val => {
                this.blood -= val
            })
        }

    }
    new Vue({
        el: '#app',
        components: {
            oneComponent,
            twoComponent
        }
    })
</script>

</html>